import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.pylab import *
import warnings
warnings.filterwarnings('ignore')

all_df=pd.read_csv('train_titanic.csv')

all_df.loc[all_df['Fare'].isnull(),'Fare']=32.180730

print(all_df.info())

train_x=all_df.loc[all_df['Age'].notnull(),['Survived', 'Sex', 'Pclass', 'Fare']]
test_x=all_df.loc[all_df['Age'].isnull(),['Survived', 'Sex', 'Pclass', 'Fare']]

from sklearn.preprocessing import StandardScaler,OneHotEncoder
from sklearn.linear_model import LinearRegression
onehot=OneHotEncoder()
x_onehot_train=onehot.fit_transform(train_x[['Survived', 'Sex', 'Pclass']]).toarray()
x_onehot_test=onehot.fit_transform(test_x[['Survived', 'Sex', 'Pclass']]).toarray()

std=StandardScaler()
x_std_train=std.fit_transform(train_x[['Fare']])
x_std_test=std.fit_transform(test_x[['Fare']])

train_x=np.c_[x_onehot_train,x_std_train]
test_x=np.c_[x_onehot_test,x_std_test]

train_y=all_df.loc[all_df['Age'].notnull(),['Age']]
model=LinearRegression()
model.fit(train_x,train_y)

y_=model.predict(test_x)

all_df.loc[all_df['Age'].isnull(),'Age']=y_
print(all_df.info())